package com.lv.xulin.demopro;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.lv.adapter.RvLinkManCheckAdapter;
import com.lv.db.entity.LinkMan;
import com.lv.tools.view.XRecyclerView;
import com.lv.tools.view.XSideBar;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class LinkmanCheckedListActivity extends Activity {
    private final String TAG = this.getClass().getSimpleName().toString();


    private XSideBar sideBarPinyin;
    private XRecyclerView xRecyclerView;
    private RvLinkManCheckAdapter mAdapter;//适配器
    private RelativeLayout rltBottom;
    private TextView tvCheckedCount;
    private Button btnSubmit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_linkman_check);

        String checkedMan = getIntent().getStringExtra("checkedMan");
        List<LinkMan> list = JSONArray.parseArray(checkedMan, LinkMan.class);
        Collections.sort(list, new Comparator<LinkMan>() {
            @Override
            public int compare(LinkMan lhs, LinkMan rhs) {
                return lhs.getFname().compareTo(rhs.getFname());
            }
        });
        //显示联系人
        showLinkMan(list);
        checkState();
    }

    private void showLinkMan(List<LinkMan> list) {

        tvCheckedCount = (TextView) findViewById(R.id.tvCheckedCount);
        btnSubmit = (Button) findViewById(R.id.btnSubmit);
        rltBottom = (RelativeLayout) findViewById(R.id.rltBottom);
        //显示数据 RecyclerView
        xRecyclerView = (XRecyclerView) findViewById(R.id.rvLinkman);
        mLinearLayoutManager = new LinearLayoutManager(LinkmanCheckedListActivity.this);
        xRecyclerView.setLayoutManager(mLinearLayoutManager);
        mAdapter = new RvLinkManCheckAdapter(LinkmanCheckedListActivity.this, list, R.layout.item_linkman_checklist);
        xRecyclerView.setAdapter(mAdapter);
        mAdapter.notifyDataSetChanged();
        mAdapter.setOnRvItemClickLitener(new RvLinkManCheckAdapter.OnRvItemClickLitener() {
            @Override
            public void onItemClick(View v, int position) {
                checkState();
            }



            @Override
            public void onItemLongClick(View v, int position) {

            }
        });

        //侧边搜索栏
        sideBarPinyin = (XSideBar) findViewById(R.id.sideBarPinyin);
        sideBarPinyin.setTextView(((TextView) findViewById(R.id.dialog)));
        sideBarPinyin.OnLetterChangeListener(new XSideBar.OnLetterChangeListener() {
            @Override
            public void onTouchingLetterChanged(String s) {
                // 该字母首次出现的位置
                int position = mAdapter.getPositionForSection(s.charAt(0));
                if (position != -1) {
                    scrollToPosition(position);
                }
            }
        });

        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }
    private void checkState() {
        tvCheckedCount.setText("已选择:"+mAdapter.getItemCount()+"人");
        Intent intent=new Intent();
        intent.putExtra("removeList",JSONArray.toJSONString(mAdapter.getRemoveList()));
        setResult(RESULT_OK,intent);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return true;
    }


    private LinearLayoutManager mLinearLayoutManager;

    private void scrollToPosition(int n) {
        int firstItem = mLinearLayoutManager.findFirstVisibleItemPosition();
        int lastItem = mLinearLayoutManager.findLastVisibleItemPosition();
        if (n <= firstItem) {
            xRecyclerView.scrollToPosition(n);
        } else if (n <= lastItem) {
            int top = xRecyclerView.getChildAt(n - firstItem).getTop();
            xRecyclerView.smoothScrollBy(0, top);
        } else {
            xRecyclerView.scrollToPosition(n);
        }
    }

}